//
//  QSJsonParser.swift
//  TestSwift
//
//  Created by Song on 2018/4/19.
//  Copyright © 2018年 Song. All rights reserved.
//

import UIKit

class QSJsonParser: NSObject {
    /// 把JsonData转换为对象(数组或字典)
    ///
    /// - Parameter jsonData: Json数据
    /// - Returns: Json对象
    class func qs_parserJsonDataToObject(jsonData: Data) -> Any? {
        let jsonObj = try? JSONSerialization.jsonObject(with: jsonData, options: JSONSerialization.ReadingOptions.mutableContainers)
        
        return jsonObj
    }
    
    /// JsonString转换为字典
    ///
    /// - Parameter jsonString: Json字符串
    /// - Returns: 字典
    class func qs_parserJsonStringToDictionary(jsonString: String) -> Dictionary<String, Any>{
        
        let jsonData:Data = jsonString.data(using: .utf8)!
        
        let dict = self.qs_parserJsonDataToObject(jsonData: jsonData)
        if dict != nil {
            return dict as! Dictionary
        }
        return Dictionary()
    }
    
    /// JsonString转换为数组
    ///
    /// - Parameter jsonString: Json字符串
    /// - Returns: 数组
    class func qs_parserJsonStringToArray(jsonString: String) -> Array<Any>{
        
        let jsonData:Data = jsonString.data(using: .utf8)!
        
        let array = self.qs_parserJsonDataToObject(jsonData: jsonData)
        if array != nil {
            return array as! Array
        }
        return Array()
    }
    
    /// 对象转Json字符串
    ///
    /// - Parameter obj: 对象
    /// - Returns: Json字符串
    class func qs_parserObjectToJsonString(_ obj: Any) -> String {
        var jsonString: String?
        
        let jsonData = try? JSONSerialization.data(withJSONObject: obj, options: JSONSerialization.WritingOptions.prettyPrinted)
        if jsonData != nil {
            jsonString = String.init(data: jsonData!, encoding: String.Encoding.utf8)
        }
        
        // 去掉字符串中的空格
        let range: NSRange = NSRange.init(location: 0, length: (jsonString?.count)!)
        jsonString = jsonString?.replacingOccurrences(of: " ", with: "", options: String.CompareOptions.literal, range: Range.init(range, in: jsonString!))
        // 去掉字符串中的换行符
        let range1: NSRange = NSRange.init(location: 0, length: (jsonString?.count)!)
        jsonString = jsonString?.replacingOccurrences(of: "\n", with: "", options: String.CompareOptions.literal, range: Range.init(range1, in: jsonString!))
        
        return jsonString!
    }
    
    /// 把Json文件转换为对象
    ///
    /// - Parameters:
    ///   - fileName: 文件名
    ///   - fileType: 文件类型
    ///   - isMainBundleFile: 是否是MainBundle中的文件  true: fileName直接传文件名   false: fileName传文件的完整路径
    /// - Returns: 转换后的对象
    class func qs_parserJsonFileToObject(fileName: String, fileType: String?, isMainBundleFile: Bool = true) -> Any? {
        var path: String = fileName
        
        if isMainBundleFile {
            path = Bundle.main.path(forResource: fileName, ofType: fileType)!
        }
        
        if let data = NSData.init(contentsOfFile: path) {
            return self.qs_parserJsonDataToObject(jsonData: data as Data)
        }
        
        return nil
    }
}
